﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Blade.IO;
using Blade.Metadata.CliLoader;

namespace Blade.Metadata.PeLoader {
	public sealed class CliFileHeader {
		public CliFileHeader(BinaryReader reader, IMetadataLoaderContext context) {
			var baseOffset = reader.Position();
			this.HeaderSize = reader.ReadUInt32();
			this.RuntimeMajorVersion = reader.ReadUInt16();
			this.RuntimeMinorVersion = reader.ReadUInt16();
			this.MetadataRoot = new RVAPair(reader);
			this.Flags = reader.ReadUInt32();
			this.EntryPointToken = reader.ReadUInt32();
			this.Resources = new RVAPair(reader);
			this.StrongNameSignature = new RVAPair(reader);
			this.CodeManagerTable = new RVAPair(reader); // == 0
			context.Assert(() => CodeManagerTable == 0);
			this.VTableFixups = new RVAPair(reader);
			this.ExportAddressTableJumps = new RVAPair(reader); // == 0
			context.Assert(() => ExportAddressTableJumps == 0);
			this.ManagedNativeHeader = new RVAPair(reader); // == 0
			context.Assert(() => ManagedNativeHeader == 0);
			var length = reader.Position() - baseOffset;
			context.Assert(() => HeaderSize == length);
		}

		public readonly uint HeaderSize;
		public readonly ushort RuntimeMajorVersion;
		public readonly ushort RuntimeMinorVersion;
		public readonly RVAPair MetadataRoot;
		public readonly uint Flags;
		public readonly uint EntryPointToken;
		public readonly RVAPair Resources;
		public readonly RVAPair StrongNameSignature;
		public readonly RVAPair CodeManagerTable; // == 0
		public readonly RVAPair VTableFixups;
		public readonly RVAPair ExportAddressTableJumps; // == 0
		public readonly RVAPair ManagedNativeHeader; // == 0
	}
}
